package com.industrial.app.controller;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.industrial.app.domain.BizGycpcxl;
import com.industrial.app.service.IBizGycpcxlService;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 工业产品销售率Controller
 *
 * @author SangGuangCan
 * @date 2024-01-24
 */
@RestController
@RequestMapping("/biz/gycpcxl")
public class BizGycpcxlController extends BaseController
{
    @Autowired
    private IBizGycpcxlService bizGycpcxlService;

    @Value("${app.file-upload-path}")
    private String fileUploadPath;


    /**
     * 查询工业产品销售率列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:gycpcxl:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizGycpcxl bizGycpcxl)
    {
        startPage();
        List<BizGycpcxl> list = bizGycpcxlService.selectBizGycpcxlList(bizGycpcxl);
        return getDataTable(list);
    }

    /**
     * 导出工业产品销售率列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:gycpcxl:export')")
//    @Log(title = "工业产品销售率", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizGycpcxl bizGycpcxl)
    {
        List<BizGycpcxl> list = bizGycpcxlService.selectBizGycpcxlList(bizGycpcxl);
        ExcelUtil<BizGycpcxl> util = new ExcelUtil<BizGycpcxl>(BizGycpcxl.class);
        util.exportExcel(response, list, "工业产品销售率数据");
    }

    /**
     * 获取工业产品销售率详细信息
     */
//    @PreAuthorize("@ss.hasPermi('biz:gycpcxl:query')")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(bizGycpcxlService.selectBizGycpcxlByBaseId(baseId));
    }

    /**
     * 新增工业产品销售率
     */
//    @PreAuthorize("@ss.hasPermi('biz:gycpcxl:add')")
//    @Log(title = "工业产品销售率", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizGycpcxl bizGycpcxl)
    {
        return toAjax(bizGycpcxlService.insertBizGycpcxl(bizGycpcxl));
    }

    /**
     * 修改工业产品销售率
     */
//    @PreAuthorize("@ss.hasPermi('biz:gycpcxl:edit')")
//    @Log(title = "工业产品销售率", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizGycpcxl bizGycpcxl)
    {
        return toAjax(bizGycpcxlService.updateBizGycpcxl(bizGycpcxl));
    }

    /**
     * 删除工业产品销售率
     */
//    @PreAuthorize("@ss.hasPermi('biz:gycpcxl:remove')")
//    @Log(title = "工业产品销售率", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(bizGycpcxlService.deleteBizGycpcxlByBaseIds(baseIds));
    }

    /**
     * <p>
     *     导入工业产品销售率
     * </p>
     * @param params 前端传来的参数，目前只有一个月份
     * @param multipartFile 上传的Excel文件
     * @return 导入是否成功
     */
    @PostMapping("/importExcel")
    public AjaxResult importExcel(@RequestParam Map<String,String> params, @RequestPart("file") MultipartFile multipartFile){
        try{
            String yearMonth = params.get("yearMonth");
            if(StringUtils.isBlank(yearMonth)){
                return AjaxResult.warn("请选择导入的年月");
            }

            List<BizGycpcxl> list = bizGycpcxlService.selectBizGycpcxlList(new BizGycpcxl());
            if(list != null && !list.isEmpty()){
                return AjaxResult.warn("导入数据之前需要清空历史数据");
            }
            /* 上传文件的路径 */

            String absoluteExcelPath = fileUploadPath + File.separator + IdUtil.simpleUUID() + ".xlsx";
            File file = new File(absoluteExcelPath);
            if(!file.exists()){
                FileUtil.touch(file);
            }
            multipartFile.transferTo(file);

//            String uploadPath = FileUploadUtils.upload(AppConfig.getProfile(),multipartFile);
//            String absoluteExcelPath = FileUploadUtils.replaceAbsolutePath(uploadPath);
            bizGycpcxlService.importExcel(absoluteExcelPath,yearMonth);
            return AjaxResult.success();
        }catch (IOException e){
            return AjaxResult.error("导入失败");
        }
    }

    /**
     * <p>
     *     清空工业产品销售率
     * </p>
     * @return
     */
    @DeleteMapping("/clearAllTableData")
    public AjaxResult clearAllTableData(){
        bizGycpcxlService.clearAllTableData();
        return AjaxResult.success();
    }

    /**
     * <p>
     *     将临时表中的数据迁移到正式表虽
     * </p>
     * @return
     */
    @PostMapping("/transferDataInOneClick")
    public AjaxResult transferDataInOneClick(){
        return toAjax(bizGycpcxlService.transferDataInOneClick());
    }

}
